#include "rc.tab.hpp"
#include "rc.routine.h"
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
int _yyerror(YYREENTRANT_PARAM const char* fmt ,...)
{
  va_list ap;
  va_start(ap , fmt);
 // fprintf(stderr , "%d:error: ",yylineno);
  vfprintf(stderr, fmt , ap);
  fprintf(stderr , "\n");
}

int rcerror(const char* fmt ,...)
{
  va_list ap;
  va_start(ap , fmt);
 // fprintf(stderr , "%d:error: ",yylineno);
  vfprintf(stderr, fmt , ap);
  fprintf(stderr , "\n");
}

static const char* nodetypename[ntype_max] ={

  [0] = NULL,
  [ntype_gt]          = "cmp_gt",
  [ntype_lt]          = "cmp_lt",
  [ntype_ne]          = "cmp_ne",
  [ntype_eq]          = "cmp_eq",
  [ntype_ge]          = "cmp_ge",
  [ntype_le]          = "cmp_le",
  [7] = NULL,
  // undefine 0007;
  [ntype_or]          = "logic_or",
  [ntype_nand]        = "logic_nand",
  [ntype_and]         = "logic_and",
  [ntype_not]         = "logic_not",

  [12] = NULL,
  [13] = NULL,
  [14] = NULL,
  [15] = NULL,
  [16] = NULL,
  [ntype_int]         = "type_int",
  [ntype_float]       = "type_float",
  [ntype_fn]          = "type_function",
  [ntype_name]        = "type_name",
  [ntype_str]         = "type_string",
  [ntype_lst]         = "type_list",

};

const char* gettypename(int type)
{
    if (type < ntype_max)
    {
        return nodetypename[type];
    }
    return NULL;
}


// type : seed enum nodetype
struct ast* newast(int type , struct ast* l, struct ast* r)
{
  struct ast *a = (struct ast *)malloc(sizeof(struct ast));
  if ( !a ) { rcerror("out of space");exit(0);} 
  a->nodetype = type;
  a->l = l;
  a->r = r;
  return a;
}

struct ast* newnum(long v)
{
  struct intval *a = (struct intval *)malloc(sizeof(struct intval));
  if ( !a ) { rcerror("out of space");exit(0);} 
  a->nodetype = ntype_int;
  a->val = v;
  return (struct ast*)a;
}

struct ast* newfloat(double v)
{
  struct floatval *a = (struct floatval*)malloc(sizeof(struct floatval));
  if ( !a ) { rcerror("out of space");exit(0);} 
  a->nodetype = ntype_float;
  a->val = v;
  return (struct ast*)a;
}

struct ast* newfunc(int functype , struct ast* n,struct ast* l )
{
  struct fncall *a = (struct fncall *)malloc(sizeof(struct fncall));
  if ( !a ) { rcerror("out of space");exit(0);}
  a->nodetype = ntype_fn;
  a->n = n;
  a->l = l;
  return (struct ast*)a;
}
struct ast* newlist (struct ast* l )
{
  struct arglist *a = (struct arglist*)malloc(sizeof(struct arglist));
  if ( !a ) { rcerror("out of space");exit(0);} 
  a->nodetype = ntype_lst;
  a->v = l;
  a->n = NULL;
  a->cnt = 1;
  return (struct ast*) a;
}

struct ast* addtolist (struct ast* lst , struct ast* add )
{
  struct arglist * l = (struct arglist *) lst ; 
  struct arglist *a = (struct arglist *)newlist(add);
  a->n = l;
  a->cnt = l->cnt + 1 ;
  return (struct ast*)a;
}

struct ast* newstr(int nodetype, char* v )
{
  struct strval *a = (struct strval *)malloc(sizeof(struct strval) + strlen(v));
  if ( !a ) { rcerror("out of space");exit(0);}
  a->nodetype = nodetype;
  strcpy(a->val , v) ;
  return (struct ast*)a;
}



